//
//  ScrollSprite.m
//  StarBugs
//
//  Created by ?? ? on 6/25/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Sprite/ScrollSprite.h"
#include "Util/ModuleController.h"
#include "Util/Util.h"

using namespace cocos2d;

ScrollSprite *ScrollSprite::pScrollSprite = NULL;

ScrollSprite::ScrollSprite()
{
//    winSize = CCSize(320, 480);
    winSize = CCDirector::sharedDirector()->getWinSize();
    if(ModuleController::getIsIpad())
        winSize = CCSizeMake(winSize.width/2, winSize.height/2);
    
    scrollBar = CCSprite::create("scroll_bar.png");
    scrollBar->setPosition(ccp(winSize.width - 15, winSize.height/2 + 30));
    scrollBar->setOpacity(0);
    addChild(scrollBar);
    
    scrollButton = CCSprite::create("scroll_tap.png");
    scrollButton->setOpacity(0);
    scrollButton->setPosition(ccp(winSize.width - 15, winSize.height/2 -120));
    addChild(scrollButton);
}

ScrollSprite::~ScrollSprite()
{
    
}

void ScrollSprite::setIsShow(bool flg)
{
    isShow = flg;
    setVisible(flg);
}

void ScrollSprite::showScroll()
{
//    CCFadeTo *fade = CCFadeTo::create(0.3, 255);
//    scrollBar->runAction(fade);
//    
//    fade = CCFadeTo::create(0.3, 255);
//    scrollButton->runAction(fade);
}

void ScrollSprite::hideScroll()
{
//    CCFadeTo *fade = CCFadeTo::create(0.3, 0);
//    scrollBar->runAction(fade);
//    
//    fade = CCFadeTo::create(0.3, 0);
//    scrollButton->runAction(fade);
}

float ScrollSprite::setButtonPos(cocos2d::CCPoint pt)
{
    CCPoint pos;
    pos.x = winSize.width - 15;
    pos.y = MIN(winSize.height/2 + 180, MAX(winSize.height/2 -120, pt.y / ModuleController::getImageScale()));
    scrollButton->setPosition(pos);
    
    return (pos.y - (winSize.height/2 -120)) / 300.f;
}

void ScrollSprite::setButtonRate(float rate)
{
    CCPoint pos;
    pos.x = winSize.width - 15;
    pos.y = winSize.height/2 -120 + 300 * rate;
    scrollButton->setPosition(pos);
}

bool ScrollSprite::getIsTouchButton(cocos2d::CCPoint pt)
{
    return CCRectMake(scrollButton->getPosition().x - 10 * ModuleController::getImageScale(),
                                              scrollButton->getPosition().y - 30 * ModuleController::getImageScale(),
                                              20 * ModuleController::getImageScale(),
                                              60 * ModuleController::getImageScale()).containsPoint(pt);
}

void ScrollSprite::touchEndedButton()
{
    CCMoveTo *move = CCMoveTo::create(0.5, ccp((winSize.width - 15) * ModuleController::getImageScale(),
                                                           (winSize.height/2 -120) * ModuleController::getImageScale()) );
    scrollButton->runAction(move);
}